package com.cx.common.core.mapstrcut;

import java.sql.Timestamp;
import java.time.*;
import java.util.Date;
import java.util.Objects;

public class EpochMillisConvertorMapper {
    public EpochMillisConvertorMapper() {
    }


    public long asInt64(Date date) {
        return Objects.isNull(date) ? 0L : date.getTime();
    }

    public long asInt64(Timestamp date) {
        return Objects.isNull(date) ? 0L : date.getTime();
    }

    public long asInt64(ZonedDateTime zdt) {
        return Objects.isNull(zdt) ? 0L : zdt.toInstant().toEpochMilli();
    }

    public long asInt64(Instant inst) {
        return Objects.isNull(inst) ? 0L : inst.toEpochMilli();
    }

    public long asInt64(OffsetDateTime odt) {
        return Objects.isNull(odt) ? 0L : odt.toInstant().toEpochMilli();
    }

    public ZonedDateTime asZonedDateTime(long epochMilli) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    }

    public Instant asInstant(long epochMilli) {
        return epochMilli <= 0L ? null : Instant.ofEpochMilli(epochMilli);
    }

    public Date asDate(long epochMilli) {
        return new Date(epochMilli);
    }

    public Timestamp asTimestamp(long epochMilli) {
        return new Timestamp(epochMilli);
    }

    public OffsetDateTime asOffsetDateTime(long epochMilli) {
        return OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    }

    public Long asInt64(LocalDateTime localDateTime) {
        return Objects.isNull(localDateTime) ? 0L : localDateTime.toEpochSecond(ZoneOffset.of("+8")) * 1000L;
    }

    public LocalDateTime asLocaleDateTime(Long epochMills) {
        return epochMills != null && epochMills > 0L ? LocalDateTime.ofEpochSecond(epochMills / 1000L, 0, ZoneOffset.ofHours(8)) : null;
    }
}
